2 using System.Collections.Generic;
5 using Microsoft.Xna.Framework.Input;
7 namespace SuperPolarity
9 static class InputController
11 static Dictionary<string, List<Action<float>>> Listeners;
12 static Dictionary<string, List<Keys>> RegisteredKeys;
13 static Dictionary<string, List<Buttons>> RegisteredButtons;
15 static GamePadState InputGamePadState;
16 static KeyboardState InputKeyboardState;
21 * You register: name of the event (ie. attack) and a key associated with it.
22 * or button... Left Stick /always/ dispatches move event.
25 static InputController()
27 Listeners = new Dictionary<string,List<Action<float>>>();
28 InputKeyboardState = new KeyboardState();
29 InputGamePadState = new GamePadState();
32 public static void UpdateInput()
36 DispatchRegisteredEvents();
39 private static void Poll()
41 InputGamePadState = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);
42 InputKeyboardState = Keyboard.GetState();
45 private static void DispatchRegisteredEvents()
49 private static void DispatchMoveEvents()
51 float xMovement = 0.0f;
52 float yMovement = 0.0f;
53 // Dispatch the moveX / MoveY events every frame.
55 xMovement = InputGamePadState.ThumbSticks.Left.X;
56 yMovement = -InputGamePadState.ThumbSticks.Left.Y;
58 Console.WriteLine("Dispatching Input {0}", InputKeyboardState.IsKeyDown(Keys.Left));
60 if (InputKeyboardState.IsKeyDown(Keys.Left))
65 if (InputKeyboardState.IsKeyDown(Keys.Right))
70 if (InputKeyboardState.IsKeyDown(Keys.Up))
75 if (InputKeyboardState.IsKeyDown(Keys.Down))
80 Dispatch("moveX", xMovement);
81 Dispatch("moveY", yMovement);
84 public static void Bind(string eventName, Action<float> listener)
86 List<Action<float>> newListenerList;
87 List<Action<float>> listenerList;
90 if (!Listeners.ContainsKey(eventName)) {
91 newListenerList = new List<Action<float>>();
92 Listeners.Add(eventName, newListenerList);
95 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
97 listenerList.Add(listener);
100 public static void Dispatch(string eventName, float value)
102 List<Action<float>> listenerList;
105 foundListeners = Listeners.TryGetValue(eventName, out listenerList);
112 foreach (Action<float> method in listenerList)